package de.web.gui;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;

import de.web.gui.model.AnbieterDataProvider;
import de.web.gui.model.GuiAnbieter;

public class AnbieterGui extends WebPage
{

   public AnbieterGui()
   {
      add(new StyleSheetReference("stylesheet",
         AnbieterGui.class, "webcss.css"));
      
      add(new InputForm("inputForm"));
      createAnbieterList();
   }

   private void createAnbieterList()
   {
      List<GuiAnbieter> list =  ((AnbieterApplication) getApplication()).getAnbieterDataProvider().loadAll();
      
      final PageableListView<GuiAnbieter> dataView = new PageableListView<GuiAnbieter>("simple",  list,10)
      {   
         @Override
         protected void populateItem(ListItem<GuiAnbieter> item)
         {
            GuiAnbieter user = (GuiAnbieter) item.getModelObject();
            item.add(new Label("id", user.getName()));
            item.add(new Label("idurl", user.getUrl()));
         }
      };

      add(dataView);

      add(new PagingNavigator("navigator", dataView));

   }

   private class InputForm extends Form<GuiAnbieter>
   {
      public InputForm(String id)
      {
         super(id, new CompoundPropertyModel<GuiAnbieter>(new GuiAnbieter()));

         add(new TextField<String>("name").setRequired(true).setLabel(new Model<String>("String")));

         add(new TextField<String>("url").setRequired(true));

         add(new Button("saveButton"));

      }

      @Override
      public void onSubmit()
      {
         // Form validation successful. Display message showing edited model.
         GuiAnbieter model = (GuiAnbieter) getDefaultModelObject();
         System.out.println("Saved model " + getDefaultModelObject());
        
         AnbieterDataProvider anbieterDao = getAnbieterDao();

         anbieterDao.save(model);
         setResponsePage(AnbieterGui.class);
      }

   }

   public AnbieterDataProvider getAnbieterDao()
   {
      return ((AnbieterApplication) getApplication()).getAnbieterDataProvider();
   }
}
